package org.robot.work.web20190121;

import java.util.List;

import org.jsoup.helper.StringUtil;
import org.robot.Spider;
import org.robot.component.Page;
import org.robot.dao.AuthorDao;
import org.robot.dao.IssueDao;
import org.robot.dao.JournalDao;
import org.robot.dao.PaperDao;
import org.robot.pojo.Author;
import org.robot.pojo.Issue;
import org.robot.pojo.Journal;
import org.robot.pojo.Paper;
import org.robot.utils.RegexUtil;

/**
 * @author pdz
 * @date 2019年1月21日
 */
public class Engineering extends Spider {
	private static final Long TASKID = 1001L;

	@Override
	public void parse(Page page) throws Exception {
		if (page.typeEquals("journal")) {
//			parseJournal(page);
			page.addNext(RegexUtil.regexFirst("http.+shtml", page.select("script").html()), "journal_real");
		} else if (page.typeEquals("archive")) {
			page.setNextLinksFilter(e -> !StringUtil.isBlank(e.text()) && RegexUtil.getYear(e.text()).matches("201[6-9]"));
		}
		else if (page.typeEquals("issue")) {
//			parseIssue(page);
		} else if (page.typeEquals("paper")) {
//			parsePaper(page);
//			parsePaperInfo(page);
		}
		
	}
	

	private void parsePaperInfo(Page page) {
		List<String> list = page.select("meta[name^=citation_author]").eachAttr("content");
		Long paperid = PaperDao.selectPidByWebsite(Paper.class, page.url(), TASKID);
		boolean hasEmail = false;
		for (int i = list.size() - 1; i >= 0; i--) {
			String str = list.get(i);
			Author author = new Author();
			if (str.contains("@")) {
				hasEmail = true;
				continue;
			}
			if (hasEmail) {
				author.setEmail(list.get(i+1));
				hasEmail = false;
			}
			author.setFullName(str);
			author.setTaskId(TASKID);
			author.setPaperid(paperid);
			AuthorDao.addAuthor(author);
		}
	}


	private void parsePaper(Page page) {
		String title = page.select("meta[property=og:title]").attr("content");
		String doi = page.select("meta[name=citation_doi]").attr("content");
		String pdfUrl = page.select("meta[name=citation_pdf_url]").attr("content");
		String abst = page.select(".abstract_block").text();
		Long issueid = IssueDao.selectPidByWebsite(Issue.class, page.prevUrl(), TASKID);
		
		Paper paper = new Paper();
		paper.setWebsite(page.url());
		paper.setTitle(title);
		paper.setTaskid(TASKID);
		paper.setDoi(doi);
		paper.setAbstracts(abst);
		paper.setPdfUrl(pdfUrl);
		paper.setIssueId(issueid);
		PaperDao.addPaper(paper);
	}

	private void parseIssue(Page page) {
		Long journalid = JournalDao.selectPidByWebsite(Journal.class, page.prevUrl(), TASKID);
		String year = (String) page.get("year");
		String vol = RegexUtil.regexFirst("\\d+", page.urlText());
		Issue issue = new Issue();
		issue.setIssue("Vol " + vol);
		issue.setYear(year);
		issue.setTaskId(TASKID);
		issue.setWebsite(page.url());
		issue.setJournalId(journalid);
		IssueDao.addIssue(issue);
	}

	private void parseJournal(Page page) {
		String text = page.select("caption").text();
		List<String> issnList = RegexUtil.regex("\\w{4}-\\w{4}", text);
		Journal journal = new Journal();
		journal.setWebsite(page.url());
		journal.setIssn(issnList.get(0));
		journal.setIssnonline(issnList.get(1));
		journal.setTaskId(TASKID);
		journal.setJournalname(page.urlText());
		JournalDao.addJournal(journal);
	}

	public static void main(String[] args) {
		Engineering claw = new Engineering();
		claw.setBaseDirectory("E:/webSpider");
		claw.addSeed("http://engineering.ckcest.cn/cae/EN/cae/home.shtml");
		claw.addSelector("div.sic_news_leftmenu ul li a", "journal");
		claw.addByHand();
		//http://journal.hep.com.cn/fese/EN/article/showVolumnList.do
		claw.addRegex("http.+article/show.*VolumnList.do", "archive");
		//http://engineering.ckcest.cn/eng/EN/volumn/volumn_3222.shtml
		claw.addRegex("http.+EN/volumn/volumn_\\d+.shtml", "issue");
		claw.addSelector("a.txt_biaoti", "paper");
		
		claw.start();
		
		
	}

}
